<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Service;
use app\common\model\service\Category;
use app\common\model\service\Order;
use app\api\validate\CarersOrder as CarersOrderValidate;
use app\common\model\service\order\Info;
use app\common\model\service\order\Refuse;
use think\Db;
use think\Exception;

class CarersOrder extends Api
{
    protected $noNeedRight = '*';

    public function lists()
    {
        $page = $this->request->get('page', 1);
        $size = $this->request->get('size', 10);
        $status = $this->request->get('status', 1);

        $where = [];
        $where['status'] = $status;
        $where['escort_id'] = $this->auth->id;

        $count = Order::where($where)->count();
        $list = Order::with(['jz'])->where($where)->order('id desc')->page($page, $size)->select();

        $data = [
            'page' => $page,
            'size' => $size,
            'count' => $count,
            'list' => $list
        ];

        $this->success('获取成功', $data);
    }

    /**
     * 订单详情
     */
    public function detail()
    {
        $order_id = $this->request->get('order_id', 0);
        if (!$order = Order::with(['jz', 'hospital', 'info'])->find($order_id)) {
            $this->error('订单不存在');
        }
        $order['total_renew_time'] = Info::where(['order_id' => $order['id'], 'status' => 1])->whereNull('paytime')->where('renewal_amount', '>', 0)->sum('time');
        $order['total_renew_commission'] = Info::where(['order_id' => $order['id'], 'status' => 1])->whereNull('paytime')->where('renewal_amount', '>', 0)->sum('commission');

        $order['cur_commission'] = Info::where(['order_id' => $order['id'], 'status' => 0])->sum('commission');
        $order['renewal_commission'] = Info::where(['order_id' => $order['id'], 'status' => 1])->sum('commission');
        $this->success('获取成功', $order);
    }

    /**
     * 拒绝派单
     */
    public function refuse()
    {
        $data = $this->request->post();

        $validate = validate(CarersOrderValidate::class);
        if (!$validate->scene('refuse')->check($data)) {
            $this->error($validate->getError());
        }

        Db::startTrans();
        try {
            $order = Order::get($data['order_id'], ['escort', 'user']);

            //保存拒绝信息
            Refuse::create([
                'escort_id' => $this->auth->id,
                'mark' => $data['reason'],
                'createtime' => time(),
                'order_id' => $order['id']
            ]);
            Order::unbind_telephone($order->escort->mobile, $order->user->mobile, $order->id);
            //更新订单信息
            $order->escort_id = 0;
            $order->status = 1;
            $order->save();

            Db::commit();
            $this->success('已拒绝接单');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 接单
     */
    public function receive()
    {
        $data = $this->request->post();

        $validate = validate(CarersOrderValidate::class);
        if (!$validate->scene('receive')->check($data)) {
            $this->error($validate->getError());
        }

        Db::startTrans();
        try {
            $order = Order::get($data['order_id']);
            //更新订单信息
            $order->status = 2;
            $order->save();

            Db::commit();
            $this->success('接单成功');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 开始服务
     */
    public function start()
    {
        $data = $this->request->post();

        $validate = validate(CarersOrderValidate::class);
        if (!$validate->scene('start')->check($data)) {
            $this->error($validate->getError());
        }

        Db::startTrans();
        try {
            $order = Order::get($data['order_id']);
            //更新订单信息
            $order->status = 3;
            $order->servicetime = time();
            $order->save();

            Db::commit();
            $this->success('操作成功');
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 计算续费金额
     */
    public function calcRenewal()
    {
        $order_id = $this->request->get('order_id');
        $time = $this->request->get('time');
        $order = Order::get($order_id);
        $service = Service::get($order->cate_id);
        $pay_amount = bcmul($service->renewal, $time, 2);
//        if ($service->category_ids) {
//            $service_category = Category::get($order->category_id);
//            $pay_amount = bcmul($service_category->renewal, $time, 2);
//        }
        $pay_amount = number_format($pay_amount * $this->auth->getUser()['proportion'] / 100, 2);

        $this->success('计算成功', $pay_amount);
    }
}
